<template>
  <el-dialog :title="!dataForm.id ? '新建' :'编辑'"
             :close-on-click-modal="false" append-to-body
             :visible.sync="visible" class="JNPF-dialog JNPF-dialog_center" lock-scroll
             width="1200px">
    <el-row :gutter="15" class="">
      <el-form ref="elForm" :model="dataForm" :rules="rules" size="small" label-width="150px" label-position="right">
        <template v-if="!loading">
          <el-col :span="8">
            <el-form-item label="资产名称"
                          prop="assetName">
              <span v-if="dataForm.id">        {{dataForm.assetName}}</span>
              <span v-if="!dataForm.id">      <popupTableSelect   v-model="dataForm.assetName"
                                  placeholder="请选择"  clearable  filterable   field="assetName" interfaceId="612769091110831365" :columnOptions="assetNamecolumnOptions" propsValue="id" relationField="zcname" popupType="popover"
                                  popupTitle="选择数据"  popupWidth="800px"
                                  hasPage :pageSize="20"   >

              </popupTableSelect>
</span>

            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="评估基准日"
                          prop="valuationDate">
              <el-date-picker v-model="dataForm.valuationDate"
                              placeholder="请选择" clearable :style='{"width":"100%"}' type="date" format="yyyy-MM-dd"
                              value-format="timestamp">

              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="评估报告日"
                          prop="evaluationDate">
              <el-date-picker v-model="dataForm.evaluationDate"
                              placeholder="请选择" clearable :style='{"width":"100%"}' type="date" format="yyyy-MM-dd"
                              value-format="timestamp">

              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="评估机构"
                          prop="evaluationAgency">
<!--              <el-select v-model="dataForm.evaluationAgency"-->
<!--                         placeholder="请选择" clearable :style='{"width":"100%"}'>-->
<!--                <el-option v-for="(item, index) in evaluationAgencyOptions" :key="index" :label="item.fullName"-->
<!--                           :value="item.id" :disabled="item.disabled"></el-option>-->

<!--              </el-select>-->

              <el-input v-model="dataForm.evaluationAgency"
                        placeholder="请输入评估机构"    >

              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="评估公允总价值(万元)"
                          prop="evaluateValue">
              <el-input-number v-model="dataForm.evaluateValue"
                               placeholder="数字文本" :step="1">

              </el-input-number>

            </el-form-item>
          </el-col>
<!--          <el-col :span="8">-->
<!--            <el-form-item label="公允单位"-->
<!--                          prop="evaluationUnit">-->
<!--              <el-input-number v-model="dataForm.evaluationUnit"-->
<!--                               placeholder="数字文本" :step="1">-->

<!--              </el-input-number>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="12">
            <el-form-item label="评估机构"
                          prop="evaluationAgency1">
              <!--              <el-select v-model="dataForm.evaluationAgency"-->
              <!--                         placeholder="请选择" clearable :style='{"width":"100%"}'>-->
              <!--                <el-option v-for="(item, index) in evaluationAgencyOptions" :key="index" :label="item.fullName"-->
              <!--                           :value="item.id" :disabled="item.disabled"></el-option>-->

              <!--              </el-select>-->

              <el-input v-model="dataForm.evaluationAgency1"
                        placeholder="请输入评估机构"    >

              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公允市场租金"
                          prop="evaluateMarketRent">
              <el-input-number v-model="dataForm.evaluateMarketRent"
                               placeholder="数字文本" :step="1">

              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="备注"
                          prop="notes">
              <el-input v-model="dataForm.notes"
                        placeholder="请输入" :style='{"width":"100%"}' true type="textarea"
                        :autosize='{"minRows":4,"maxRows":4}'>

              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="评估附件"
                          prop="evaluateFile">
              <JNPF-UploadFz v-model="dataForm.evaluateFile"
                             :fileSize="10" sizeUnit="MB" :limit="9" buttonText="点击上传">

              </JNPF-UploadFz>
            </el-form-item>
          </el-col>
        </template>
      </el-form>
      <SelectDialog v-if="selectDialogVisible" :config="currTableConf" :formData="dataForm"
                    ref="selectDialog" @select="addForSelect" @close="selectDialogVisible=false"/>
    </el-row>
    <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false"> 取 消</el-button>
        <el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading"> 确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import request from '@/utils/request'
import {getDataInterfaceRes} from '@/api/systemData/dataInterface'
import {getDictionaryDataSelector} from '@/api/systemData/dictionary'

export default {
  components: {},
  props: [],
  data() {
    return {
      visible: false,
      loading: false,
      btnLoading: false,
      selectDialogVisible: false,
      currTableConf: {},
      addTableConf: {},
      tableRows: {},
      currVmodel: "",
      dataForm: {
        assetName: "",
        valuationDate: '',
        evaluationDate: '',
        evaluationAgency: "",
        evaluationAgency1: "",
        evaluateValue: 0,
        evaluationUnit: undefined,
        evaluateMarketRent: undefined,
        notes: '',
        evaluateFile: [],
      },
      rules:
        {},
      assetNameOptions: [{"fullName": "选项一", "id": "1"}, {"fullName": "选项二", "id": "2"}],
      evaluationAgencyOptions: [],
      assetNamecolumnOptions:[
        {
          value: 'id',
          label: '资产名称'
        },
        {
        value: 'zcname',
        label: '资产名称'
      }
        ,
        {
          value: 'zcnumber',
          label: '资产编码'
        },

        {
          value: 'address',
          label: '地址'
        }]

    }
  },
  computed: {},
  watch: {},
  created() {
    this.getevaluationAgencyOptions();
  },
  mounted() {
  },
  methods: {
    getevaluationAgencyOptions() {
      getDictionaryDataSelector('600000000000000003').then(res => {
        this.evaluationAgencyOptions = res.data.list
      })
    },
    clearData() {
      this.dataForm.assetName = "";
      this.dataForm.valuationDate = '';
      this.dataForm.evaluationDate = '';
      this.dataForm.evaluationAgency = "";
      this.dataForm.evaluateValue = 0;
      this.dataForm.evaluationUnit = undefined,
        this.dataForm.evaluateMarketRent = undefined,
        this.dataForm.notes = '';
      this.dataForm.evaluateFile = [];
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields();
        if (this.dataForm.id) {
          this.loading = true
          request({
            url: '/api/example/ZcglZcrzZcpg/' + this.dataForm.id,
            method: 'get'
          }).then(res => {
            this.dataInfo(res.data)
            this.loading = false
          });
        } else {
          this.clearData()
        }
      });
      this.$store.commit('generator/UPDATE_RELATION_DATA', {})
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['elForm'].validate((valid) => {
        if (valid) {
          this.request()
        }
      })
    },
    request() {
      this.btnLoading = true
      let _data = this.dataList()
      if (!this.dataForm.id) {
        request({
          url: '/api/example/ZcglZcrzZcpg',
          method: 'post',
          data: _data
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: 'success',
            duration: 1000,
            onClose: () => {
              this.visible = false
              this.btnLoading = false
              this.$emit('refresh', true)
            }
          })
        }).catch(() => {
          this.btnLoading = false
        })
      } else {
        request({
          url: '/api/example/ZcglZcrzZcpg/' + this.dataForm.id,
          method: 'PUT',
          data: _data
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: 'success',
            duration: 1000,
            onClose: () => {
              this.visible = false
              this.btnLoading = false
              this.$emit('refresh', true)
            }
          })
        }).catch(() => {
          this.btnLoading = false
        })
      }
    },
    openSelectDialog(key) {
      this.currTableConf = this.addTableConf[key]
      this.currVmodel = key
      this.selectDialogVisible = true
      this.$nextTick(() => {
        this.$refs.selectDialog.init()
      })
    },
    addForSelect(data) {
      for (let i = 0; i < data.length; i++) {
        let item = {...this.tableRows[this.currVmodel], ...data[i]}
        this.dataForm[this.currVmodel].push(item)
      }
    },
    dataList() {
      var _data = JSON.parse(JSON.stringify(this.dataForm));
      _data.evaluateFile = Array.isArray(_data.evaluateFile) ? JSON.stringify(_data.evaluateFile) : '[]'
      return _data;
    },
    dataInfo(dataAll) {
      let _dataAll = dataAll
      _dataAll.evaluateFile = _dataAll.evaluateFile ? JSON.parse(_dataAll.evaluateFile) : []
      this.dataForm = _dataAll
    },
  },
}

</script>
